<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formLabels"
      label-width="80px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="标签名称" prop="labelname">
            <el-input v-model="formData.labelname" placeholder="请输入标签名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="唯一标识" prop="tamplateid">
            <el-input v-model="formData.tamplateid"
          /></el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="父类名称" prop="fathername">
            <el-input v-model="formData.fathername" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="formData.sort" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import * as BiaoqianApi from '@/api/system/biaoqian'
import { number } from 'vue-types'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用

const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: number,
  labelname: '',
  fathername: '',
  tamplateid: '',
  sort: undefined,
  fatherid: number
})

// 设置表单必填项验证
const formLabels = reactive({
  type: [{ required: true, message: '请假类型不能为空', trigger: 'change' }],
  labelname: [{ required: true, message: '标签名称不能为空', trigger: 'blur' }],
  tamplateid: [{ required: true, message: '标签标识不能为空', trigger: 'blur' }],
  sort: [
    { pattern: /^([1-9]|[1-9][0-9]|100)$/, message: '请输入1到100之间的数字', trigger: 'blur' }
  ]
})
const formRef = ref() // 表单 Ref
/** 打开弹窗 */
const open = async (type: string, data: BiaoqianApi.LabelTreeVo, id?: number) => {
  listBiaoqianApiParams.nameList = data['nameList']
  dialogVisible.value = true //弹窗是否打开
  dialogTitle.value = t('action.' + type) // 弹窗标题
  formType.value = type // 表单的类型：create - 新增；update - 修改
  resetForm()

  if (id == 0) {
    formData.value.fathername = '标签管理'
    formData.value.fatherid = 0
  } else if (id == 1) {
    formData.value.fathername = '分类管理'
    formData.value.fatherid = 1
  } else {
    if (type == 'create') {
      formData.value = await BiaoqianApi.getLabelFatherById(id as number)
    } else {
      formData.value = await BiaoqianApi.getLabelById(id as number)
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 标签查询
const listBiaoqianApiParams = reactive({
  nameList: '',
  labelname: ''
})

const emits = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BiaoqianApi.LabelVO
    if (formType.value === 'create') {
      await BiaoqianApi.createLabel(data)
      // 触发标签树形结构 方法
      message.success(t('common.createSuccess'))
    } else {
      await BiaoqianApi.updateLabel(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
  } finally {
    emits('success', listBiaoqianApiParams)
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    labelname: '',
    fathername: '',
    tamplateid: '',
    sort: undefined,
    fatherid: undefined
  }
  formRef.value?.resetFields()
}
/** 关键字展示 */
const openKeywords = ref()
</script>
